<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Resources</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="plugins.html" title="Chapter 18. Extending Smarty With Plugins">
<link rel="prev" href="plugins.outputfilters.html" title="Output Filters">
<link rel="next" href="plugins.inserts.html" title="Inserts">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Resources</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="plugins.outputfilters.html">Prev</a> </td>
<th width="60%" align="center">Chapter 18. Extending Smarty With Plugins</th>
<td width="20%" align="right"> <a accesskey="n" href="plugins.inserts.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="Resources">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="plugins.resources"></a>Resources</h2></div></div></div>
<p>
     Resource plugins are meant as a generic way of providing template
     sources or PHP script components to Smarty. Some examples of resources:
     databases, LDAP, shared memory, sockets, and so on.
    </p>
<p>
	  Custom Resources may be put in a file <code class="literal">resource.foobarxyz.php</code> within your 
	  <a class="link" href="variable.plugins.dir.html" title="$plugins_dir"><code class="varname">$plugins_dir</code></a>, or registered on runtime
	  with <a class="link" href="api.register.resource.html" title="registerResource()"><code class="varname">registerResource()</code></a>. 
	  In either case you will be able to access that resource by prepending its name to the template you're addressing: 
	  <code class="literal">foobarxyz:yourtemplate.tpl</code>.
	</p>
<p>
		If a Resource's templates should not be run through the Smarty compiler, the Custom Resource may extend 
		<code class="literal">Smarty_Resource_Uncompiled</code>. The Resource Handler must then implement the function 
		<code class="literal">renderUncompiled(Smarty_Internal_Template $_template)</code>. 
		<code class="varname">$_template</code> is a reference to the current template and contains all assigned variables 
		which the implementor can access via <code class="literal">$_template-&gt;smarty-&gt;getTemplateVars()</code>. 
		
		These Resources simply echo their rendered content to the output stream. The rendered output will be 
		output-cached if the Smarty instance was configured accordingly. 
		
		See <code class="literal">libs/sysplugins/smarty_internal_resource_php.php</code> for an example.
	</p>
<p>
		If the Resource's compiled templates should not be cached on disk, the Custom Resource may extend 
		<code class="literal">Smarty_Resource_Recompiled</code>. These Resources are compiled every time they are accessed. 
		This may be an expensive overhead. 
		
		See <code class="literal">libs/sysplugins/smarty_internal_resource_eval.php</code> for an example.
	</p>
<div class="example">
<a name="id465726"></a><p class="title"><b>Example 18.10. Using custom resources</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php

/**
 * MySQL Resource
 *
 * Resource Implementation based on the Custom API to use
 * MySQL as the storage resource for Smarty's templates and configs.
 *
 * Table definition:
 * &lt;pre&gt;CREATE TABLE IF NOT EXISTS `templates` (
 *   `name` varchar(100) NOT NULL,
 *   `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 *   `source` text,
 *   PRIMARY KEY (`name`)
 * ) ENGINE=InnoDB DEFAULT CHARSET=utf8;&lt;/pre&gt;
 *
 * Demo data:
 * &lt;pre&gt;INSERT INTO `templates` (`name`, `modified`, `source`) VALUES ('test.tpl', "2010-12-25 22:00:00", '{$x="hello world"}{$x}');&lt;/pre&gt;
 *
 * @package Resource-examples
 * @author Rodney Rehm
 */
class Smarty_Resource_Mysql extends Smarty_Resource_Custom {
    // PDO instance
    protected $db;
    // prepared fetch() statement
    protected $fetch;
    // prepared fetchTimestamp() statement
    protected $mtime;

    public function __construct() {
        try {
            $this-&gt;db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty", "smarty");
        } catch (PDOException $e) {
            throw new SmartyException('Mysql Resource failed: ' . $e-&gt;getMessage());
        }
        $this-&gt;fetch = $this-&gt;db-&gt;prepare('SELECT modified, source FROM templates WHERE name = :name');
        $this-&gt;mtime = $this-&gt;db-&gt;prepare('SELECT modified FROM templates WHERE name = :name');
    }
    
    /**
     * Fetch a template and its modification time from database
     *
     * @param string $name template name
     * @param string $source template source
     * @param integer $mtime template modification timestamp (epoch)
     * @return void
     */
    protected function fetch($name, &amp;$source, &amp;$mtime)
    {
        $this-&gt;fetch-&gt;execute(array('name' =&gt; $name));
        $row = $this-&gt;fetch-&gt;fetch();
        $this-&gt;fetch-&gt;closeCursor();
        if ($row) {
            $source = $row['source'];
            $mtime = strtotime($row['modified']);
        } else {
            $source = null;
            $mtime = null;
        }
    }
    
    /**
     * Fetch a template's modification time from database
     *
     * @note implementing this method is optional. Only implement it if modification times can be accessed faster than loading the comple template source.
     * @param string $name template name
     * @return integer timestamp (epoch) the template was modified
     */
    protected function fetchTimestamp($name) {
        $this-&gt;mtime-&gt;execute(array('name' =&gt; $name));
        $mtime = $this-&gt;mtime-&gt;fetchColumn();
        $this-&gt;mtime-&gt;closeCursor();
        return strtotime($mtime);
    }
}


require_once 'libs/Smarty.class.php';
$smarty = new Smarty();
$smarty-&gt;registerResource('mysql', new Smarty_Resource_Mysql());

// using resource from php script
$smarty-&gt;display("mysql:index.tpl");
?&gt;

     </pre>
<p>
      And from within Smarty template:
     </p>
<pre class="programlisting">

{include file='mysql:extras/navigation.tpl'}

     </pre>
</div>
</div>
<br class="example-break"><p>
 See also
 <a class="link" href="api.register.resource.html" title="registerResource()"><code class="varname">registerResource()</code></a>,
 <a class="link" href="api.unregister.resource.html" title="unregisterResource()"><code class="varname">unregisterResource()</code></a>.
</p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="plugins.outputfilters.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="plugins.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="plugins.inserts.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Output Filters </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Inserts</td>
</tr>
</table>
</div>
</body>
</html>
